using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MediatR;
using RBAC.Read.Api.Application.Command.ERP.WarehouseManagement;
using RBAC.ErrorCode;
using RBAC.Domain.WarehouseManagement;
using RBAC.Read.Api.Application.Dto.ERP;

namespace RBAC.Read.Api.Controllers.ERP
{
    /// <summary>
    /// 仓库管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WarehouseManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        public WarehouseManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取仓库列表（分页）
        /// </summary>
        /// <param name="request">查询参数</param>
        /// <returns>仓库列表</returns>
        [HttpGet("GetWarehouseList")]
        public async Task<APIResult<APIPaging<WarehouseList>>> GetWarehouseList([FromQuery] GetWarehouseListCommands request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 根据Id获取仓库信息（反填）
        /// </summary>
        /// <param name="request">包含仓库Id的请求</param>
        /// <returns>仓库信息</returns>
        [HttpGet("GetWarehouseById")]
        public async Task<APIResult<WarehouseList>> GetWarehouseById([FromQuery] GetWarehouseByIdCommands request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 仓库模糊查询（返回Id与名称）
        /// </summary>
        /// <param name="request">关键词与Top数量</param>
        /// <returns>仓库选项列表</returns>
        [HttpGet("SearchWarehouse")]
        public Task<APIResult<List<WarehouseOptionDto>>> SearchWarehouse([FromQuery] SearchWarehouseCommand request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 打印仓库列表（不分页，供前端打印）
        /// </summary>
        /// <param name="request">可选名称过滤</param>
        /// <returns>仓库列表</returns>
        [HttpGet("PrintWarehouseList")]
        public Task<APIResult<List<WarehouseList>>> PrintWarehouseList([FromQuery] GetprintWarehouseCommands request)
        {
            return _mediator.Send(request);
        }

        /// <summary>
        /// 导出仓库列表到Excel
        /// </summary>
        /// <param name="request">导出参数</param>
        /// <returns>Excel文件</returns>
        [HttpGet("ExportWarehouseList")]
        public async Task<IActionResult> ExportWarehouseList([FromQuery] ExportWarehouseListCommands request)
        {
            var result = await _mediator.Send(request);

            if (result.Code == APIEnums.Success && result.Data.Length > 0)
            {
                var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
                var fileName = $"仓库列表_{ts}.xlsx";
                const string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                // 直接通过框架提供的 File 重载返回，避免手动设置响应头导致的兼容问题
                return File(result.Data, contentType, fileName);
            }
            else
            {
                return BadRequest(result);
            }
        }
    }
}
